import os
# -*- coding:utf-8 -*-
# @Author: shenyuyu
# @Time: 2023/6/13 9:34
# @File: student_manager.py

"""
学生管理系统
"""

class student:
    def __init__(self, name, sex, age):
        self.name = name
        self.sex = sex
        self.age = age



def menu():
    print("------欢迎来到学生管理系统------")
    print("------1.添加学生--------------")
    print("------2.删除学生--------------")
    print("------3.修改学生--------------")
    print("------4.查询学生--------------")
    print("------5.显示所有学生-----------")
    print("------6.退出系统--------------")


def save():
    f = open("students.txt", "w", encoding="utf8")
    f.write(str(students))
    f.close()


def add():
    d = {"name": input("请输入学生姓名："), "sex": input("请输入学生性别："), "age": input("请输入学生年龄：")}
    students.append(str(d))
    save()
    print("添加成功！")


def delete():
    name = input("请输入要删除学生的姓名：")
    for i in students:
        if i.get("name") == name:
            students.remove(i)
            print("删除成功！")


def update():
    name = input("请输入要修改学生的姓名：")
    for i in students:
        if i.get("name") == name:
            i["name"] = input("请输入学生姓名：")
            i["sex"] = input("请输入学生性别：")
            i["age"] = input("请输入学生年龄：")
            print("修改成功！")


def select():
    name = input("请输入要查询学生的姓名：")
    for i in students:
        if i.get("name") == name:
            print("学生姓名：" + i["name"])
            print("学生性别：" + i["sex"])
            print("学生年龄：" + i["age"])


def select_all():
    for i in students:
        print("学生姓名：" + eval(i)["name"])
        print("学生性别：" + eval(i)["sex"])
        print("学生年龄：" + eval(i)["age"])
    # f = open("students.txt", "r", encoding="utf-8")
    # while f.readable():
    #     readline = f.readline()
    #     if readline == "":
    #         break
    #     else:
    #         split = readline.split(" ")
    #         student = {}
    #         student["name"] = split[0]
    #         student["sex"] = split[1]
    #         student["age"] = split[2]
    #         students.append(student)
    # f.close()


def read_data():
    global students
    if os.path.exists("students.txt"):
        f = open("students.txt", "r", encoding="utf8")
        read = f.read()
        if read:
            students = eval(read)
        f.close()


students = []


while 1 == 1:
    menu()
    read_data()
    a = int(input("请输入需求编号："))
    if a == 1:
        add()
    elif a == 2:
        delete()
    elif a == 3:
        update()
    elif a == 4:
        select()
    elif a == 5:
        select_all()
    else:
        break

# 学生管理系统持久化  未完...
